package com.example.viewtest.draw_6;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.View;
import android.widget.RelativeLayout;

import com.example.viewtest.Utils;

/**
 * 文字绘制 自适应布局内的图片
 */
public class TextViewLayout extends RelativeLayout {
    private static final String TAG = "TextViewLayout";
    private static final String text = "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of \"de Finibus Bonorum et Malorum\" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, \"Lorem ipsum dolor sit amet..\", comes from a line in section 1.10.32.\n" +
            "\n" +
            "The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from \"de Finibus Bonorum et Malorum\" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.";

    private TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    private int fontSpacing;
    private SparseArray<Rect> childViewLine = new SparseArray<>();

    {
        paint.setTextSize(Utils.db2px(20));
        fontSpacing = (int) paint.getFontSpacing();
    }

    public TextViewLayout(Context context) {
        this(context, null);
    }

    public TextViewLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TextViewLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setWillNotDraw(false);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View childAt = getChildAt(i);
            int left = childAt.getLeft();
            int top = childAt.getTop();
            int right = childAt.getRight();
            int bottom = childAt.getBottom();

            int startLine = top / fontSpacing;
            int endLine = bottom / fontSpacing + (bottom % fontSpacing == 0 ? 0 : 1);
            for (int j = startLine; j < endLine; j++) {
                Rect rect = childViewLine.get(j);
                if (rect != null) {
                    left = left < rect.left ? left : rect.left;
                    right = right > rect.right ? right : rect.right;
                }
                childViewLine.put(j, new Rect(left, j * fontSpacing, right, (j + 1) * fontSpacing));
            }
        }
    }



    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        //要绘制的开始字符
        int satrtCharIndex = 0;
        int lineCount = getHeight() / fontSpacing;
        for (int i = 0; i < lineCount; i++) {
            Rect rect = childViewLine.get(i);
            if (rect != null) {
                if (rect.left > 0) {
                    //绘制左边
                    String str = getString(satrtCharIndex, rect.left);
                    canvas.drawText(str, 0, (i + 1) * fontSpacing, paint);
                    satrtCharIndex += str.length();
                }
                if (rect.right < width) {
                    //绘制右边
                    String str = getString(satrtCharIndex, width-rect.right);
                    canvas.drawText(str, rect.right, (i + 1) * fontSpacing, paint);
                    satrtCharIndex += str.length();
                }
            } else {
                //绘制整行
                String str = getString(satrtCharIndex, width);
                canvas.drawText(str, 0, (i + 1) * fontSpacing, paint);
                satrtCharIndex += str.length();
            }
        }


    }


    private String getString(int startIndex, int width) {
        int count = paint.breakText(text, startIndex, text.length(), true, width, null);
        return text.substring(startIndex,startIndex+count);
    }

}
